#include "SPI.h"

/* ********************************************************** */
/*!	
	\brief Initializes spi0 as master.

	\note Selects hardware or software SPI based on constant
	 defintion SPI0_SOFT.

	\author Joe Pardue 
	\date October 29, 2010
	\version 1.00
*/
/* ********************************************************** */
void spi0_init_master(void)
{
	#if defined (SPI0_SOFT)
	spi0_soft_init_master();
	#else // HARD
	spi0_hard_init_master();
	#endif
}

/*****************************************************
	Software SPI
******************************************************/
/* ********************************************************** */
/*!	
	\brief Initializes a software SPI for spi0.

	\author Joe Pardue 
	\date October 29, 2010
	\version 1.00
*/
/* ********************************************************** */
void spi0_soft_init_master()
{
  	// Initialize MISO as input
  	// set DDR pin register to 0
  	miso_ddr &= ~(1<<miso_port_pin);

  	// Initialize MOSI, SCLK, AND /SS as outputs
  	// set DDR pin registers to 1
    mosi_ddr |= (1<<mosi_port_pin);
	sclk_ddr |= (1<<sclk_port_pin);
  	ss_ddr |= (1<<ss_port_pin); 
}

/*****************************************************
	Hardware SPI
******************************************************/

/* ********************************************************** */
/*!	
	\brief Initializes the AVR hardware SPI for spi0

	\author Joe Pardue 
	\date October 29, 2010
	\version 1.00
*/
/* ********************************************************** */
void spi0_hard_init_master(void)
{
	PORTB |= (1<< miso_hardware_pin) \
 			| (1<< mosi_hardware_pin) \
 			| (1<< sclk_hardware_pin) \
 			| (1<< ss_hardware_pin);

	//Set MOSI, SCK AND SS to outputs
	DDRB |= (1<< mosi_hardware_ddr) \
			| (1<< sclk_hardware_ddr) \
			| (1<< ss_hardware_ddr);

	// Set Miso to input
	DDRB &= ~(1<<miso_hardware_ddr);

    SPCR = ( 1 << SPE ) | ( 1 << MSTR ) | ( 1 << SPR0 ); 
}

/* ********************************************************** */
